﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Yugi.Client.Card;
using System.Threading;

namespace Yugi.Client.Control
{
	/// <summary>
	/// Interaction logic for YCard.xaml
	/// </summary>
	public partial class YCard : UserControl
	{
        CardModel model;
        YTrangThaiLaBai state, last;

		public YCard()
		{
			this.InitializeComponent();
            IsViewDetail = true;
            IsBaiDoiThu = false;
            this.Loaded += new RoutedEventHandler(YCard_Loaded);
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(YCard_DataContextChanged);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(Card_MouseLeftButtonDown);
           
        }

    
        public YCard(YTrangThaiLaBai state)
        {
            this.InitializeComponent();
            IsViewDetail = true;
            IsBaiDoiThu = false;
            if (state == YTrangThaiLaBai.Defend_Down || state== YTrangThaiLaBai.Attact_Down)
                this.txt_thongtin.Visibility = Visibility.Hidden;
            //Update State
            VisualStateManager.GoToState(this, state.ToString(), false);
            this.state = state;
            //Update Hanler
            this.Loaded += new RoutedEventHandler(YCard_Loaded);
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(YCard_DataContextChanged);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(Card_MouseLeftButtonDown);
           
        }
        //-----------------------------------Event Handler--------------------------------
        #region Event Handler

       
        void YCard_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            model = this.DataContext as CardModel;
            if (model == null) return;
            this.img.ImageSource = model.YCard.displayImage;
            if (model.YCard.CardType == YType.Monster)
            {
               
                 YMonsterCard card = model.YCard as YMonsterCard;
                 this.ThongTin =card.Damage + " | " + card.Defend;
               
               
            }
        }

        void YCard_Loaded(object sender, RoutedEventArgs e)
        {
            //int val = 12;
            //this.Margin = new Thickness(2, 0, 2, 2);
            //this.IsDown = false;
            
        }
       

        #endregion
        //-------------------------------------------------------------------
        private void Card_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!IsViewDetail) return;
            YCard img = sender as YCard;
            this.model.YSupervisorModel.ReceiveMessageFromChildren(ClientMSG.PrintCard, img.model.YCard as YBaseCard);
        }

        public void SetCong(int new_value)
        {
            YMonsterCard temp=this.model.YCard as YMonsterCard;
            if (temp != null)
            {
                temp.Damage = new_value;
                this.ThongTin = temp.Damage + " | " + temp.Defend;
            }
        }

        public void SetThu(int new_value)
        {
            YMonsterCard temp = this.model.YCard as YMonsterCard;
            if (temp != null)
            {
                temp.Defend = new_value;
                this.ThongTin = temp.Damage + " | " + temp.Defend;
            }
        }

        public void ShowActiveIcon()
        {
            this.image1.Visibility = Visibility.Visible;
        }

        public void CloseActiveIcon()
        {
            this.image1.Visibility = Visibility.Collapsed;
        }
        public void ShowCanChoice()
        {
            this.rectangle.Opacity = 0.5;
        }
        public void HideCanChoice()
        {
            this.rectangle.Opacity = 0;
        }
        #region Property


        /// <summary>
        /// Kiem tra xem la bai dang up hay mo
        /// </summary>
        bool isdown;
        public bool IsDown { get { return isdown; } set { isdown = value; } }

        /// <summary>
        /// Kiem Tra xem duoc phep xem thong tin la bai hay khong
        /// </summary>
        public bool IsViewDetail { get; set; }

        /// <summary>
        /// Kiem Tra xem phai bai doi thu hay khong
        /// </summary>
        public bool IsBaiDoiThu { get; set; }
        /// <summary>
        /// Trang Thai cua la bai hien tai( Dung de doi trang thai va Animation)
        /// </summary>
        public YTrangThaiLaBai State
        {
            get
            {
                return this.state;
            }
            set
            {
                this.state = value;

                if (value == YTrangThaiLaBai.Ritual && IsDown == true)
                    this.state = YTrangThaiLaBai.Ritual_Down;
                VisualStateManager.GoToState(this, state.ToString(), true);

                this.state = value;
                try
                {
                    string s = value.ToString().Split('_')[1];
                    if (s == "Up")
                    {
                        IsDown = false;
                        this.txt_thongtin.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        IsDown = true;
                        this.txt_thongtin.Visibility = Visibility.Hidden;
                    }
                }
                
                catch (Exception ex)
                {
                }
            }
        }
        
        public string ThongTin 
        {
            get
            {
                return this.txt_thongtin.Text;
            }
            set
            {
                
                this.txt_thongtin.Text = value;
            }
        }
        public bool DaTanCong { get; set; }
        #endregion


    }
    
    public enum YTrangThaiLaBai
    {
        Attact_Up, Attact_Down, Defend_Up, Defend_Down, View_Detail,Select,Delete,Ritual,Ritual_Down
    }
}